package com.shd.edanpei.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.constraint.Group;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.OrderDetailsModel;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.enums.RoleEnum;
import com.shd.edanpei.image.LoadImageCtrl;
import com.shd.edanpei.enums.OrderTypeEnum;
import com.shd.edanpei.enums.StatusEnum;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by mc on 2018/9/20.
 * 我的订单 状态
 */

public class MeOrderStatusView extends LinearLayout {

    private final Context mContext;
    private OrderDetailsModel model;
    private Unbinder mUnbinder;

    @BindView(R.id.iv_type)
    ImageView ivType;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.group)
    Group group;
    @BindView(R.id.iv_head)
    CircleImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.planLine_view)
    PlanLineView planLine;

    public MeOrderStatusView(Context context, OrderDetailsModel model) {
        super(context);
        this.mContext = context;
        this.model = model;
        initView();

    }


    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.meorder_status, this);
        RxBus.get().register(this);
        LoadImageCtrl loadImageCtrl = new LoadImageCtrl(getContext());
        mUnbinder = ButterKnife.bind(this);
        if (model.getBaseInfo().getOrderType() == OrderTypeEnum.car.getKey()) {
            ivType.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_label_car));
            tvType.setText(OrderTypeEnum.car.getValue());
        } else if (model.getBaseInfo().getOrderType() == OrderTypeEnum.house.getKey()) {
            ivType.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_label_house));
            tvType.setText(OrderTypeEnum.house.getValue());
        }else if (model.getBaseInfo().getOrderType() == OrderTypeEnum.personal.getKey()) {
            ivType.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.xinyong));
            tvType.setText(OrderTypeEnum.personal.getValue());
        }else if (model.getBaseInfo().getOrderType() == OrderTypeEnum.house.getKey()) {
            ivType.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.qiye));
            tvType.setText(OrderTypeEnum.enterprise.getValue());
        }
        if (model.getManager() != null) {
            if (!TextUtils.isEmpty(model.getManager().getAvatarUrl())) {
                loadImageCtrl.getGlide().load(model.getManager().getAvatarUrl()).into(ivHead);
            }
            tvName.setText(model.getManager().getRealName());
            tvTime.setText(model.getManager().getRobTime());
        }

        int status = model.getBaseInfo().getUserStatus();
        if (DaoManager.getInstance().getUserBean().getRoleType() == RoleEnum.user.getId()) {
            tvStatus.setText(StatusEnum.getCDes(status));
        } else {
            tvStatus.setText(StatusEnum.getUserDes(status));
        }
        if (status == StatusEnum.unpaid.getStatus()
                || status == StatusEnum.applying.getStatus()
                || status == StatusEnum.error.getStatus()
                || status == StatusEnum.cancel.getStatus()
                || status == StatusEnum.repaySuccess.getStatus()) {
            group.setVisibility(GONE);
        } else if (status == StatusEnum.first.getStatus()
                || status == StatusEnum.last.getStatus()
                || status == StatusEnum.waitService.getStatus()
                || status == StatusEnum.waitLoan.getStatus()
                || status == StatusEnum.waitfirm.getStatus()
                || status == StatusEnum.success.getStatus()
                || status == StatusEnum.refuse.getStatus()
                || status == StatusEnum.waitRepay.getStatus()) {
            group.setVisibility(VISIBLE);
            planLine.setData(status);
        }

    }


    @Subscribe(code = RxBusCode.REFRESH_ORDER_STATUS)
    public void refreshStatus(StatusBean bean) {
        int status = bean.getOrderStatus();
        tvStatus.setText(StatusEnum.getUserDes(status));
        if (status == StatusEnum.waitRepay.getStatus()) {
            //显示退款流程
            group.setVisibility(VISIBLE);
        }
        planLine.setData(status);

    }

    public void destory() {
        RxBus.get().unRegister(this);
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }

}
